package cn.bx.spring.inter;

import cn.bx.spring.dao.UserDao;
import cn.bx.spring.entity.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;

import java.util.Arrays;
@Service
@Aspect
public class UserInterceptor {
    //环绕拦截器
    @Around("within(cn.bx.spring.dao..*)")
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("方法【"+jp.getSignature().getName()+"】在"+new java.util.Date()+"开始执行");
        System.out.println("方法参数有："+ Arrays.toString(jp.getArgs()));
        if (jp.getTarget() instanceof UserDao && jp.getSignature().getName().equals("add")){
            User user =(User)jp.getArgs()[0];
            if (user.getName().length()<2||user.getNo().length()<2){
                throw  new RuntimeException("name+no属性太短");
            }
        }
        Object o=jp.proceed();//放行
        System.out.println("方法执行返回了、、、"+o);
        return o;

    }
}
